Use your distribution’s package manager
For a first local C++ program on Linux, use your distribution’s packaged GCC C++ driver, g++. GCC is the compiler collection; g++ is the command that compiles C++ and arranges C++ library linking. It is often available on Linux, but it may not be installed on a fresh system. You also need the matching standard-library headers and link components that distribution packages normally bring in as dependencies.
Ubuntu and Debian use apt; Fedora uses dnf. Follow only the command for your distribution. Do not run both sets or copy an Ubuntu command into Fedora. Package names and available compiler releases vary by distribution and release, so avoid hardcoding a supposedly universal GCC version.
Ubuntu and Debian: install G++
On Ubuntu or Debian, refresh package metadata, then install g++. This is the minimum C++ compiler path for the single-file example in this guide. If you also want common development tools such as make and packaging headers, the build-essential metapackage is a convenient broader option and depends on g++; you do not need both installation commands for this exercise.
The package manager may request your account password and confirmation. After installation, g++ --version reports the installed driver version. If your school or workplace manages the machine, follow its package policy rather than forcing a different compiler release.
Ubuntu or Debian: minimum C++ compiler installation
sudo apt update
sudo apt install g++Optional broader development bundle instead of the g++ command
sudo apt install build-essentialFedora: install GCC C++ support
On Fedora, install the gcc-c++ package with dnf. Fedora’s developer documentation names this package for the g++ driver, and the package manager resolves its required dependencies. Run g++ --version afterward to confirm which release is available. A plain gcc installation is not a reliable substitute for the separate C++ support package.
The instructions for other Linux distributions can differ. Use your distribution’s current documentation for its package name, compiler versions and repository policy rather than treating apt or dnf as universal Linux commands.
Fedora only
sudo dnf install gcc-c++
g++ --versionWrite, compile and run your first file
Create a directory, open a text editor and save the complete code below as hello.cpp. Use a plain-text file with the .cpp extension. The program writes Hello, Codectionary! to the terminal. In a terminal, change into the directory containing the file, then run the build and run commands below. A build with no errors creates an executable named hello.
The -std=c++17 option selects the C++17 language mode. -Wall and -Wextra enable common warnings, and -o hello names the output file. ./hello runs the file from the current directory. If you edit hello.cpp, rebuild it before running again; the previous executable does not change when the source changes.
hello.cpp
#include <iostream>
int main() {
std::cout << "Hello, Codectionary!" << '\n';
return 0;
}In the directory containing hello.cpp
g++ --version
g++ -std=c++17 -Wall -Wextra hello.cpp -o hello
./helloClang is an alternative, not another requirement
Clang can compile the same C++ source. On Ubuntu or Debian, the default clang package provides clang++; Fedora also packages Clang as clang. Install it only if you want to use Clang or your project calls for it. On Linux, Clang typically relies on a suitable system linker and C++ standard-library development files; keeping your distribution’s GCC C++ development packages available can simplify this first setup.
Run clang++ --version to inspect what you installed, then compile with clang++ -std=c++17 hello.cpp -o hello-clang and execute ./hello-clang. If you install an explicitly versioned Clang package, its executable may have a version suffix rather than the unsuffixed clang++ name. Check the package documentation for the version you selected.
Ubuntu or Debian alternative
sudo apt install clangFedora alternative
sudo dnf install clangAfter installing Clang
clang++ --version
clang++ -std=c++17 hello.cpp -o hello-clang
./hello-clangCommon Linux compiler and linker problems
If g++ is not found, check that you installed g++ or gcc-c++ for your distribution and opened a new terminal if your environment changed. If a standard header such as iostream is missing, verify the C++ development package and its matching standard-library headers; installing only the C compiler may not be enough. If a linker reports undefined references to C++ library symbols, use the g++ or clang++ C++ driver for the final link rather than a C-only gcc or clang command, and include every required .cpp file.
If ./hello reports no such file, check the directory, output name and whether compilation succeeded. If permission is denied, check that hello is the executable generated in your project directory; do not change system-wide permissions to work around a failed build. If an option or feature is unsupported, compare g++ --version with your distribution’s package release and the compiler’s official feature-support table.
Syntax
#include <iostream>
int main() {
std::cout << "Hello, Codectionary!" << '\n';
return 0;
}Examples
First Linux C++ terminal program
Save this code as hello.cpp, build it with your installed g++ and run ./hello.
#include <iostream>
int main() {
std::cout << "Hello, Codectionary!" << '\n';
return 0;
}- The goal is to confirm that your Linux C++ compiler, standard-library headers and linker work together on a complete program.
- #include <iostream> supplies std::cout. main starts the program, and its statements send a greeting and newline to standard output.
- The build command creates an executable called hello. Running ./hello displays Hello, Codectionary!; return 0 reports successful completion.
- Change the greeting and rebuild to see the effect. This same compile-run loop is the basis for larger projects, even when a build system later automates the commands.
Best practices
- Use your own distribution’s package manager and install only one recommended C++ compiler to start.
- Check g++ --version and select an explicit standard mode when building examples.
- Investigate a failed build before running an older executable or changing unrelated system permissions.
Runtime & versions
Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ISO C++ resources