Skip to main content

Command Palette

Search for a command to run...

How to Run an Executable in Linux

Updated
7 min read

Running an executable in Linux might seem tricky if you're new to the system. But once you understand the basics, it becomes straightforward. Whether you downloaded a program or compiled your own, knowing how to run executables is essential for using Linux efficiently.

In this article, I’ll guide you through the process step-by-step. You’ll learn how to check permissions, run executables from the terminal, and troubleshoot common issues. By the end, you’ll feel confident running any executable file on your Linux system.

Understanding Executable Files in Linux

In Linux, an executable is a file that your system can run as a program. Unlike Windows, where executables usually have a .exe extension, Linux executables don’t need a specific extension. They can be scripts, compiled programs, or binary files.

Here’s what makes a file executable in Linux:

  • File Permissions: The file must have execute permission set.
  • File Type: It can be a binary or a script with a proper interpreter.
  • Location: The file’s path matters when running it.

Linux uses a permission system to control who can read, write, or execute a file. You can check these permissions using the ls -l command in the terminal.

For example:

ls -l myprogram

This might show:

-rwxr-xr-x 1 user user 12345 Apr 27 12:00 myprogram

The x means execute permission is set for the owner, group, and others.

How to Check and Set Execute Permissions

Before running an executable, you need to ensure it has the right permissions. If it doesn’t, Linux won’t let you run it.

Checking Permissions

Use the ls -l command to see if the execute bit is set:

ls -l ./myprogram

Look for x in the permission string. If it’s missing, you need to add execute permission.

Setting Execute Permission

To add execute permission, use the chmod command:

chmod +x myprogram

This command adds execute permission for the file owner, group, and others.

If you want to limit execution to just yourself, use:

chmod u+x myprogram

After this, running ls -l again should show the x in the permissions.

Running an Executable from the Terminal

Once the file is executable, you can run it from the terminal. The method depends on where the file is located.

Running Executables in the Current Directory

If your executable is in the current directory, you need to prefix it with ./ to run it:

./myprogram

This tells the shell to look in the current directory for the executable.

Running Executables in Other Directories

If the executable is in another directory, provide the full or relative path:

/home/user/downloads/myprogram

or

../bin/myprogram

Running Executables in Your PATH

Linux uses the PATH environment variable to find executables. If your program is in a directory listed in PATH, you can run it by just typing its name:

myprogram

To see your current PATH, run:

echo $PATH

Common directories in PATH include /usr/bin, /bin, and /usr/local/bin.

If you want to add a directory to your PATH, you can edit your shell profile (~/.bashrc or ~/.zshrc) and add:

export PATH=$PATH:/home/user/myexecutables

Then reload the profile with:

source ~/.bashrc

Running Scripts as Executables

Scripts like Bash, Python, or Perl files can also be executables if they have the right permissions and a proper "shebang" line at the top.

What is a Shebang?

A shebang is the first line in a script that tells the system which interpreter to use. For example:

#!/bin/bash

or

#!/usr/bin/env python3

This line must be the very first line in the script.

Making a Script Executable

  1. Add the shebang line.
  2. Set execute permission with chmod +x scriptname.
  3. Run it with ./scriptname.

For example, a Python script named hello.py:

#!/usr/bin/env python3
print("Hello, Linux!")

Make it executable:

chmod +x hello.py

Run it:

./hello.py

Troubleshooting Common Issues

Sometimes, running executables in Linux can cause errors. Here are common problems and how to fix them.

Permission Denied

If you see:

bash: ./myprogram: Permission denied

It means the file isn’t executable. Fix it with:

chmod +x myprogram

Command Not Found

If you type the executable name and get:

bash: myprogram: command not found

It means the system can’t find the file in your PATH. Run it with ./myprogram or add its directory to your PATH.

Wrong Architecture

If you get an error like:

bash: ./myprogram: cannot execute binary file: Exec format error

The executable might be for a different CPU architecture. Check with:

file myprogram

Make sure it matches your system’s architecture (e.g., x86_64, ARM).

Missing Libraries

If the executable depends on shared libraries, you might see errors about missing files. Use:

ldd myprogram

This lists required libraries. Install missing ones using your package manager.

Running Executables with Root Privileges

Some executables require root (administrator) access to run properly.

Using sudo

Prepend sudo to run the executable as root:

sudo ./myprogram

You’ll be prompted for your password.

Switching to Root User

Alternatively, switch to the root user with:

su -

Then run the executable.

Be careful when running programs as root, as it can affect system stability and security.

Running Executables Graphically

Not all executables run in the terminal. Some have graphical interfaces.

Running GUI Programs

If the executable launches a GUI, running it from the terminal will open the window. For example:

./myguiapp

If you want to run it in the background and keep the terminal free, use:

./myguiapp &

Creating Desktop Shortcuts

You can create .desktop files to launch executables from your desktop environment menus or icons.

A simple .desktop file looks like this:

[Desktop Entry]
Name=My App
Exec=/home/user/myprogram
Type=Application
Terminal=false
Icon=/home/user/myicon.png

Save it in ~/.local/share/applications/ and it will appear in your app launcher.

Summary Table: Common Commands to Run Executables

TaskCommand ExampleNotes
Check permissionsls -l myprogramLook for x in permissions
Add execute permissionchmod +x myprogramMakes file executable
Run executable in current dir./myprogramUse ./ prefix
Run executable in PATHmyprogramIf directory is in PATH
Add directory to PATHexport PATH=$PATH:/my/dirAdd to shell profile for permanence
Check executable architecturefile myprogramVerify compatibility
Check shared librariesldd myprogramFind missing dependencies
Run as rootsudo ./myprogramRequires admin privileges

Conclusion

Running an executable in Linux is easier than it looks. You just need to check permissions, know how to run files from the terminal, and understand your system’s environment. Whether it’s a compiled program or a script, these steps will help you get it running smoothly.

Remember, Linux’s flexibility means you can run executables from anywhere, but you must handle permissions and paths carefully. With practice, running executables will become second nature, opening up many possibilities for using Linux effectively.

FAQs

How do I make a file executable in Linux?

Use the command chmod +x filename to add execute permission. This allows you to run the file as a program.

Why do I need to use ./ before the executable name?

./ tells the shell to run the executable from the current directory. Without it, the shell looks only in directories listed in your PATH.

Can I run Windows .exe files on Linux?

Not directly. You need compatibility layers like Wine to run Windows executables on Linux.

How do I add a directory to my PATH?

Edit your shell profile (like ~/.bashrc) and add export PATH=$PATH:/your/directory. Then reload with source ~/.bashrc.

What does the shebang line do in scripts?

It specifies the interpreter to run the script, like /bin/bash for Bash scripts or /usr/bin/env python3 for Python scripts.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts

How to Run an Executable in Linux