为你的 Linux 应用创建 .desktop 文件
桌面 应用
![](/data/attachment/album/201801/03/153912ci5u5i0gisbgrkkr.jpg)


在 Linux 中，一个 `.desktop` 文件就是一个用来运行程序的快捷方式。没有 `.desktop` 的话，你的应用就不会在应用菜单中显示了，也无法使用像 Synapse 和 Albert 这样的第三方启动起启动了。


大多数应用在安装后都会自动创建 `.desktop` 文件，并将自己放入应用菜单中以方便访问。然而，如果是你自己从源代码中编译的程序或者自己下载的压缩格式的应用，那就不会做这些事情了，每次你都需要打开终端来执行它的二进制文件。显然这个过程很无聊也很麻烦。


本文将会告诉你如何为应用创建 `.desktop` 文件，从而让你能在应用菜单中启动该应用。


**相关阅读**：[How to Add App Drawers to Unity Launcher in Ubuntu](https://www.maketecheasier.com/add-app-drawer-unity-launcher-ubuntu/ "How to Add App Drawers to Unity Launcher in Ubuntu")


### 如何创建桌面启动器


`.desktop` 文件基本上就是一个包含程序信息的纯文本文件，通常根据是自己可见还是所有用户可见的不同而放在 `~/.local/share/applications` 或者 `/usr/share/applications/` 目录中。你在文件管理器中访问这两个目录，都会看到很多系统中已安装应用对应的 `.desktop` 文件存在。


为了演示，我将会为 Super Tux Kart 创建一个 `.desktop` 文件，这是一个我很喜欢玩的卡丁车竞赛游戏。Ubuntu 仓库中带了这个游戏，但版本一般不新。


要获得最新的版本就需要下载 tar 包，解压并执行其中的游戏启动文件。


你可以仿照这个步骤来为任何程序创建启动器。


**注意**：下面步骤假设程序压缩包放在 “Downloads” 目录下。


1、跳转到存放压缩包的目录，右击然后选择 “Extract here”。


![application-launcher-5](/data/attachment/album/201801/03/153912p0nch8znz16x0h7v.png "application-launcher-5")


2、解压后，进入新创建的目录然后找到可执行的文件。之后右击文件选择 “Run” 来启动程序，确定程序运行正常。


![application-launcher-6](/data/attachment/album/201801/03/153912zypi7075b57fy07f.png "application-launcher-6")


3、有时候，你在右键菜单中找不到 “Run” 选项。这通常是因为这个可执行文件是一个文本文件。你可以在终端中执行它，如果你使用 GNOME 的话，可以点击上面菜单栏中的 Files 菜单，然后选择 “Preferences”。


![application-launcher-linux-1](/data/attachment/album/201801/03/153913j9zf5z9cef6495p6.png "application-launcher-linux-1")


4、选择 “Behavior” 标签页然后选择 “Executable Text Files” 下的 “Run them”。现在右击可执行文本文件后也能出现 “Run” 选项了。


![application-launcher-31](/data/attachment/album/201801/03/153913pnz3zhi2wgn5w0gg.png "application-launcher-31")


5、确认应用运行正常后，就可以退出它了。然后运行你的文本编辑器并将下面内容粘贴到空文本文件中：



```
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/path/to/executable
Name=Name of Application
Icon=/path/to/icon

```

你需要更改 “Exec” 域的值为可执行文件的路径，并且将 “Name” 域的值改成应用的名称。大多数的程序都在压缩包中提供了一个图标，不要忘记把它也填上哦。在我们这个例子中，Super Tux Kart 的启动文件看起来是这样的：


![application-launcher-supertuxkart](/data/attachment/album/201801/03/153913l7xppz2z9dgr81zz.png "application-launcher-supertuxkart")


6、将文件以 `application-name.desktop` 为名保存到 `~/.local/share/applications` 目录中。`.local` 目录位于你的家目录下，是一个隐藏目录，你需要启用 “Show Hidden Files” 模式才能看到它。如果你希望这个应用所有人都能访问，则在终端中运行下面命令：



```
sudo mv ~/.local/share/applications/<application-name`.desktop`> /usr/share/applications/

```

当然，别忘了把命令中的 `<application-name.desktop>` 改成真实的 `.desktop` 文件名。


7、完成后，打开应用菜单，就能看到应用出现在其中，可以使用了。


![application-launcher-2](/data/attachment/album/201801/03/153914euasg9q06j6qqa0w.jpg "application-launcher-2")


这个方法应该适用于所有主流的 Linux 操作系统。下面是另一张 Super Tux Kart 在 elementary OS 的应用启动器 (slingshot) 上的截图


![application-launcher-4](/data/attachment/album/201801/03/153914xrfdtb38s8sy3sc1.jpg "application-launcher-4")


如果你觉得本教程还有点用的话，欢迎留言。




---


via: <https://www.maketecheasier.com/create-desktop-file-linux/>


作者：[Ayo Isaiah](https://www.maketecheasier.com/author/ayoisaiah/) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Create Desktop File Linux 00 Featured Image](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-00-featured-image.jpg)

A .desktop file is simply a shortcut that is used to launch applications in Linux. Without the .desktop file, your application won’t show up in the Applications menu and can’t be launched with third-party launchers such as Ulauncher and Albert.

Most applications, when installed, will create the .desktop files automatically and place themselves in the “Application” menu for quick access. However, if you compile a program from source or download an app in archive format, this may not be the case, and you may have to open the terminal to execute the binary every time you want to use it. Obviously, this can become a very tedious and troublesome step. This tutorial shows how you can create a .desktop file for any application you use that can be launched from the “Application” menu.

**Also read:** [Top 6 Linux Desktop Environments of 2024](https://www.maketecheasier.com/best-desktop-environments/)

## How to Create Desktop Launchers

A .desktop file is a simple text file that holds information about a program. It is usually placed in “~/.local/share/applications” or “/usr/share/applications/,” depending on whether you want the launcher to be accessible for your local account onlly or for everyone. If you navigate to either directory in your File Manager, you will see quite a few .desktop files that correspond with the installed apps on your computer.

For demonstration purposes, we are creating a .desktop file for Super Tux Kart, a kart racing game we like to play sometimes. A version is available in the Ubuntu repos, but this is often behind the latest stable version.

The only way to get the latest and greatest release is by downloading a tar archive, extracting it and [executing a file which will launch the game](https://www.maketecheasier.com/best-linux-games/).

**Also read:** [How to Delete a Directory in Linux](https://www.maketecheasier.com/delete-directory-linux/)

You can follow along with whichever program you want to create a launcher for, and it should work the same way.

**Note**: the following steps assume you have the archive for the program you want to create a launcher for in your “Downloads” folder.

- Navigate to the directory where the archive is stored, right-click it and select “Extract here.”

![application-launcher-5 Create Desktop File Linux 01 Extract Archive](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-01-extract-archive.png)

- Once the extraction is complete, change to the newly created folder and find the executable.
- Right-click it and select “Run as a Program” to launch the program just to ensure it is working.

![application-launcher-6 Create Desktop File Linux 02 Test Run Program](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-02-test-run-program.png)

- In some cases, you won’t see the “Run” option in the menu, often because the executable is a .text file. You can get around this by executing it via the terminal.

![application-launcher-linux-1 Create Desktop File Linux 03 Run Program Terminal](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-03-run-program-terminal.png)

- If you’ve confirmed that the application works when you launch it, you can exit it.
- Launch your text editor and paste the following into the empty text file:

[Desktop Entry] Encoding=UTF-8 Version=1.0 Type=Application Terminal=false Exec=/path/to/executable Name=Name of Application Icon=/path/to/icon

**Note**: You’ll need to change the “Exec” field to the path to the executable and “Name” field to the name of the application. Most programs provide an icon somewhere in the archive, so don’t forget to include that as well. In our case, the launcher file for Super Tux Kart looks like the following image.

![application-launcher-supertuxkart Create Desktop File Linux 04 Sample Desktop File](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-04-sample-desktop-file.png)

- Save the file in the “~/.local/share/applications” folder as “application-name.desktop”. The “.local” folder is a hidden folder in your “Home” directory and “Show Hidden Files” mode will have to be enabled for you to view it. If you want it to be globally accessible, run the following command in the terminal:

sudo mv ~/.local/share/applications/<application-name.desktop> /usr/share/applications/

**Note**: don’t forget to change <application-name.desktop> to the actual name of the .desktop file.

- Once done, open the “Applications” menu on your desktop to find the .desktop app.

![application-launcher-2 Create Desktop File Linux 05 Working Desktop Shortcut](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-05-working-desktop-shortcut.png)

The method described here should work on all mainstream Linux-based operating systems. Here’s another screenshot showing Super Tux Kart in Xubuntu’s application launcher (XFCE).

![application-launcher-4 Create Desktop File Linux 06 Desktop Shortcut Xfce](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-06-desktop-shortcut-xfce.png)

**Also read:** [How to Fix the “No Installation Candidate” Problem in Ubuntu](https://www.maketecheasier.com/fix-no-installation-candidate-problem-ubuntu/)

## How to Copy .Desktop Files

As discussed above, you can obtain a .desktop file from a program that you have installed through a repository. This is helpful if you want to create a custom .desktop file as a way of [fixing a broken package in Ubuntu](https://www.maketecheasier.com/fix-broken-packages-ubuntu/).

For the most part, each Linux distribution is largely similar in its structure, so while this section will heavily focus on Ubuntu 22.04, you can still use these instructions in your favorite desktop environment.

- To start, open a File Manager from your desktop. In this case, we are opening Nautilus, as it is the default for Ubuntu.

- Press the “Other Locations” entry in Nautilus’s left side bar.

![Create Desktop File Linux 07 Sample File Manager Nautilus](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-07-sample-file-manager-nautilus.jpg)

- This brings up a list of all the system locations in your machine. Press “Computer.”

![Create Desktop File Linux 08 Open Root Filesystem](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-08-open-root-filesystem.png)

- Go to “/usr/share/applications.” This directory contains the .desktop files that your desktop manager uses to create its Application Menu.

![Create Desktop File Linux 09 Application Shortcuts Folder](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-09-application-shortcuts-folder.jpg)

- Copy the application you want to create a shortcut to. For instance, right-click on “firefox-esr.desktop” and select “Copy.”

![Create Desktop File Linux 10 Copy Desktop File](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-10-copy-desktop-file.jpg)

- Go back to your desktop and place the .desktop file by right-clicking and selecting “Paste.”

![Create Desktop File Linux 11 Paste Desktop File](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-11-paste-desktop-file.jpg)

- Right-click on your .desktop file and select “Allow Launching.” Doing this will set the permission bits so that your desktop environment can use this file to launch your program.

![Create Desktop File Linux 12 Set Launch Permissions](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-12-set-launch-permissions.jpg)

**Also read:** [How to View WebP Images on Linux](https://www.maketecheasier.com/view-webp-images-linux/)

## How to Create .Desktop Files With a Third-Party Program

Aside from editing and copying .desktop files, it is also possible to create shortcuts in Linux through a Graphical User Interface (GUI) program. This is useful if you are not comfortable with editing configuration files.

Unlike the previous methods, this will require you to install a program like Arronax, which may not be present in your distribution’s repository. Despite that, it is still possible to use it by obtaining a copy from [the developer’s website](https://www.florian-diesch.de/software/arronax/). To install the GUI in Ubuntu, follow the below instructions.

- Add a third-party repository by running the following command:

sudo add-apt-repository ppa:diesche/stable

![Create Desktop File Linux 13 Add New Repository](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-13-add-new-repository.png)

- Iinstall the GUI program by running the following command:

sudo apt install arronax

- On the other hand, if you are installing it through a tarball, you need to unpack the archive using tar:

tar xvzf /home/$USER/Downloads/arronax-0.8.1.tar.gz

![Create Desktop File Linux 14 Unpack Package Archive](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-14-unpack-package-archive.jpg)

- Run Arronax by either running the command below or typing “arronax” while in the Application Menu. This will bring up a window where you can create and save .desktop files.

./setup.py && ./run.py

![Create Desktop File Linux 15 Arronax Menu Item](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-15-arronax-menu-item.png)

- To create your first shortcut using Arronax, click the “New” icon in Arronax’s Menu Bar to create a template that you can use for your shortcut.
- Provide the shortcut’s name and a file path to your program. For example, we are creating a .desktop file for Firefox by typing “Firefox” in the “Title” field and
`/bin/firefox-esr`

in the “Command” field.

![Create Desktop File Linux 16 Arronax Sample Program](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-16-arronax-sample-program.png)

- Press the green arrow in Arronax’s Menu Bar to save the shortcut.

![Create Desktop File Linux 18 Green Arrow Highlight](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-18-green-arrow-highlight.png)

- In this example, we’re saving it as “Firefox.desktop” in our desktop directory.

![Create Desktop File Linux 17 Save Desktop File 1](https://www.maketecheasier.com/assets/uploads/2022/08/create-desktop-file-linux-17-save-desktop-file-1.png)

- Right-click this .desktop file and select “Allow Launching” to set the permission bits for it, as shown above.

**Also read:** [How to Set Up Bluetooth in Linux](https://www.maketecheasier.com/setup-bluetooth-in-linux/)

## Frequently Asked Questions

### Is it possible to use a .desktop file even if I am using a window manager?

No, a .desktop file will not work on most window managers, as it lacks the necessary support for the “XDG Menu” specification. While it is possible to create a .desktop file in a window manager, it will not show up as a desktop shortcut. One way to recreate this mechanism is by using shell scripts alongside the Simple X Hotkey Daemon.

### Is it possible to create a .desktop file for CLI and TUI applications using Arronax?

Yes! You can easily create a .desktop file for a program that runs in the terminal, which is useful if you are already using TUI programs and want to have a quick way to access them.

To do this using Arronax, create a new .desktop shortcut by providing both a “Title” and a “Command,” then toggle the “Run in Terminal” option and save the new shortcut.

### Is it possible to hide a desktop shortcut from an Application Menu?

Yes! It is possible to modify a .desktop file to not show on your Applications Menu by adding a single line of code in the file that you want to edit. For example, if you want to hide Firefox from your Applications Menu, open the “firefox-esr.desktop” file in “/usr/share/applications”. From there, add `Hidden=true`

at the end of the file, then restart your computer to apply the changes.

Image credit: Charles-Adrien Fournier via [Unsplash](https://unsplash.com/photos/g815W4LH4F8) All screenshots by Ramces Red.

Our latest tutorials delivered straight to your inbox